<html>
    <head>
       <style type="text/css">
            .hll { background-color: #ffffcc }
.c { color: #888888 } /* Comment */
.err { color: #a61717; background-color: #e3d2d2 } /* Error */
.k { color: #008800; font-weight: bold } /* Keyword */
.cm { color: #888888 } /* Comment.Multiline */
.cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.c1 { color: #888888 } /* Comment.Single */
.cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.ge { font-style: italic } /* Generic.Emph */
.gr { color: #aa0000 } /* Generic.Error */
.gh { color: #333333 } /* Generic.Heading */
.gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.go { color: #888888 } /* Generic.Output */
.gp { color: #555555 } /* Generic.Prompt */
.gs { font-weight: bold } /* Generic.Strong */
.gu { color: #666666 } /* Generic.Subheading */
.gt { color: #aa0000 } /* Generic.Traceback */
.kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.kp { color: #008800 } /* Keyword.Pseudo */
.kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.kt { color: #888888; font-weight: bold } /* Keyword.Type */
.m { color: #0000DD; font-weight: bold } /* Literal.Number */
.s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.na { color: #336699 } /* Name.Attribute */
.nb { color: #003388 } /* Name.Builtin */
.nc { color: #bb0066; font-weight: bold } /* Name.Class */
.no { color: #003366; font-weight: bold } /* Name.Constant */
.nd { color: #555555 } /* Name.Decorator */
.ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.nf { color: #0066bb; font-weight: bold } /* Name.Function */
.nl { color: #336699; font-style: italic } /* Name.Label */
.nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.py { color: #336699; font-weight: bold } /* Name.Property */
.nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.nv { color: #336699 } /* Name.Variable */
.ow { color: #008800 } /* Operator.Word */
.w { color: #bbbbbb } /* Text.Whitespace */
.mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.bp { color: #003388 } /* Name.Builtin.Pseudo */
.vc { color: #336699 } /* Name.Variable.Class */
.vg { color: #dd7700 } /* Name.Variable.Global */
.vi { color: #3333bb } /* Name.Variable.Instance */
.il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
        </style>
    </head>
    <body>

<h1>MQTT Broker</h1>
<p>The MQTT packet specific logic for the broker.</p>
<pre>
<div class="highlight"><pre><a name="MQTTBrokers.py-pyg.html-1"></a><span class="sd">&quot;&quot;&quot;</span>
<a name="MQTTBrokers.py-pyg.html-2"></a><span class="sd">*******************************************************************</span>
<a name="MQTTBrokers.py-pyg.html-3"></a><span class="sd">  Copyright (c) 2013, 2014 IBM Corp.</span>
<a name="MQTTBrokers.py-pyg.html-4"></a><span class="sd"> </span>
<a name="MQTTBrokers.py-pyg.html-5"></a><span class="sd">  All rights reserved. This program and the accompanying materials</span>
<a name="MQTTBrokers.py-pyg.html-6"></a><span class="sd">  are made available under the terms of the Eclipse Public License v1.0</span>
<a name="MQTTBrokers.py-pyg.html-7"></a><span class="sd">  and Eclipse Distribution License v1.0 which accompany this distribution. </span>
<a name="MQTTBrokers.py-pyg.html-8"></a><span class="sd"> </span>
<a name="MQTTBrokers.py-pyg.html-9"></a><span class="sd">  The Eclipse Public License is available at </span>
<a name="MQTTBrokers.py-pyg.html-10"></a><span class="sd">     http://www.eclipse.org/legal/epl-v10.html</span>
<a name="MQTTBrokers.py-pyg.html-11"></a><span class="sd">  and the Eclipse Distribution License is available at </span>
<a name="MQTTBrokers.py-pyg.html-12"></a><span class="sd">    http://www.eclipse.org/org/documents/edl-v10.php.</span>
<a name="MQTTBrokers.py-pyg.html-13"></a><span class="sd"> </span>
<a name="MQTTBrokers.py-pyg.html-14"></a><span class="sd">  Contributors:</span>
<a name="MQTTBrokers.py-pyg.html-15"></a><span class="sd">     Ian Craggs - initial implementation and/or documentation</span>
<a name="MQTTBrokers.py-pyg.html-16"></a><span class="sd">*******************************************************************</span>
<a name="MQTTBrokers.py-pyg.html-17"></a><span class="sd">&quot;&quot;&quot;</span>
<a name="MQTTBrokers.py-pyg.html-18"></a>
<a name="MQTTBrokers.py-pyg.html-19"></a><span class="kn">import</span> <span class="nn">traceback</span><span class="o">,</span> <span class="nn">random</span><span class="o">,</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">string</span><span class="o">,</span> <span class="nn">copy</span><span class="o">,</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">logging</span><span class="o">,</span> <span class="nn">socket</span><span class="o">,</span> <span class="nn">time</span>
<a name="MQTTBrokers.py-pyg.html-20"></a>
<a name="MQTTBrokers.py-pyg.html-21"></a><span class="kn">from</span> <span class="nn">..formats</span> <span class="kn">import</span> <span class="n">MQTTV311</span> <span class="k">as</span> <span class="n">MQTTV3</span>
<a name="MQTTBrokers.py-pyg.html-22"></a>
<a name="MQTTBrokers.py-pyg.html-23"></a><span class="kn">from</span> <span class="nn">.Brokers</span> <span class="kn">import</span> <span class="n">Brokers</span>
<a name="MQTTBrokers.py-pyg.html-24"></a>
<a name="MQTTBrokers.py-pyg.html-25"></a><span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&#39;MQTT broker&#39;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-26"></a>
<a name="MQTTBrokers.py-pyg.html-27"></a><span class="k">def</span> <span class="nf">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-28"></a>  <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;out: &quot;</span><span class="o">+</span><span class="nb">repr</span><span class="p">(</span><span class="n">packet</span><span class="p">))</span>
<a name="MQTTBrokers.py-pyg.html-29"></a>  <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="s">&quot;handlePacket&quot;</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-30"></a>    <span class="n">sock</span><span class="o">.</span><span class="n">handlePacket</span><span class="p">(</span><span class="n">packet</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-31"></a>  <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-32"></a>    <span class="k">try</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-33"></a>      <span class="n">sock</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">pack</span><span class="p">())</span> <span class="c"># Could get socket error on send</span>
<a name="MQTTBrokers.py-pyg.html-34"></a>    <span class="k">except</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-35"></a>      <span class="k">pass</span>
<a name="MQTTBrokers.py-pyg.html-36"></a>
<a name="MQTTBrokers.py-pyg.html-37"></a><span class="k">class</span> <span class="nc">MQTTClients</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-38"></a>
<a name="MQTTBrokers.py-pyg.html-39"></a>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">anId</span><span class="p">,</span> <span class="n">cleansession</span><span class="p">,</span> <span class="n">keepalive</span><span class="p">,</span> <span class="n">socket</span><span class="p">,</span> <span class="n">broker</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-40"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="n">anId</span> <span class="c"># required</span>
<a name="MQTTBrokers.py-pyg.html-41"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">cleansession</span> <span class="o">=</span> <span class="n">cleansession</span>
<a name="MQTTBrokers.py-pyg.html-42"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">socket</span> <span class="o">=</span> <span class="n">socket</span>
<a name="MQTTBrokers.py-pyg.html-43"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">msgid</span> <span class="o">=</span> <span class="mi">1</span>
<a name="MQTTBrokers.py-pyg.html-44"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">outbound</span> <span class="o">=</span> <span class="p">[]</span> <span class="c"># message objects - for ordering </span>
<a name="MQTTBrokers.py-pyg.html-45"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span> <span class="o">=</span> <span class="p">{}</span> <span class="c"># msgids to message objects</span>
<a name="MQTTBrokers.py-pyg.html-46"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">broker</span> <span class="o">=</span> <span class="n">broker</span>
<a name="MQTTBrokers.py-pyg.html-47"></a>    <span class="k">if</span> <span class="n">broker</span><span class="o">.</span><span class="n">publish_on_pubrel</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-48"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">inbound</span> <span class="o">=</span> <span class="p">{}</span> <span class="c"># stored inbound QoS 2 publications</span>
<a name="MQTTBrokers.py-pyg.html-49"></a>    <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-50"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">inbound</span> <span class="o">=</span> <span class="p">[]</span>
<a name="MQTTBrokers.py-pyg.html-51"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">connected</span> <span class="o">=</span> <span class="bp">False</span>
<a name="MQTTBrokers.py-pyg.html-52"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">will</span> <span class="o">=</span> <span class="bp">None</span>
<a name="MQTTBrokers.py-pyg.html-53"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">keepalive</span> <span class="o">=</span> <span class="n">keepalive</span>
<a name="MQTTBrokers.py-pyg.html-54"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">lastPacket</span> <span class="o">=</span> <span class="bp">None</span>
<a name="MQTTBrokers.py-pyg.html-55"></a>
<a name="MQTTBrokers.py-pyg.html-56"></a>  <span class="k">def</span> <span class="nf">resend</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-57"></a>    <span class="k">for</span> <span class="n">pub</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">outbound</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-58"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;resending&quot;</span><span class="p">,</span> <span class="n">pub</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-59"></a>      <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">DUP</span> <span class="o">=</span> <span class="mi">1</span>
<a name="MQTTBrokers.py-pyg.html-60"></a>      <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-61"></a>        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.1.2-3] Dup when resending QoS 1 publish id </span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-62"></a>        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.3.1-4] Message id same as original publish on resend&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-63"></a>        <span class="n">respond</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">socket</span><span class="p">,</span> <span class="n">pub</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-64"></a>      <span class="k">elif</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-65"></a>        <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">qos2state</span> <span class="o">==</span> <span class="s">&quot;PUBREC&quot;</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-66"></a>          <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.1.2-3] Dup when resending QoS 2 publish id </span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-67"></a>          <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">DUP</span> <span class="o">=</span> <span class="mi">1</span>
<a name="MQTTBrokers.py-pyg.html-68"></a>          <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.3.1-4] Message id same as original publish on resend&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-69"></a>          <span class="n">respond</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">socket</span><span class="p">,</span> <span class="n">pub</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-70"></a>        <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-71"></a>          <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Pubrels</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-72"></a>          <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.3.1-4] Message id same as original publish on resend&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-73"></a>          <span class="n">resp</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="o">=</span> <span class="n">pub</span><span class="o">.</span><span class="n">messageIdentifier</span>
<a name="MQTTBrokers.py-pyg.html-74"></a>          <span class="n">respond</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">socket</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-75"></a>
<a name="MQTTBrokers.py-pyg.html-76"></a>  <span class="k">def</span> <span class="nf">publishArrived</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">topic</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">qos</span><span class="p">,</span> <span class="n">retained</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-77"></a>    <span class="n">pub</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Publishes</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-78"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.2.3-3] topic name must match the subscription&#39;s topic filter&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-79"></a>    <span class="n">pub</span><span class="o">.</span><span class="n">topicName</span> <span class="o">=</span> <span class="n">topic</span>
<a name="MQTTBrokers.py-pyg.html-80"></a>    <span class="n">pub</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">msg</span>
<a name="MQTTBrokers.py-pyg.html-81"></a>    <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">=</span> <span class="n">qos</span>
<a name="MQTTBrokers.py-pyg.html-82"></a>    <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">RETAIN</span> <span class="o">=</span> <span class="n">retained</span>
<a name="MQTTBrokers.py-pyg.html-83"></a>    <span class="k">if</span> <span class="n">retained</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-84"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.1.2-7] Last retained message on matching topics sent on subscribe&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-85"></a>    <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">RETAIN</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-86"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.1.2-9] Set retained flag on retained messages&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-87"></a>    <span class="k">if</span> <span class="n">qos</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-88"></a>      <span class="n">pub</span><span class="o">.</span><span class="n">qos2state</span> <span class="o">=</span> <span class="s">&quot;PUBREC&quot;</span>
<a name="MQTTBrokers.py-pyg.html-89"></a>    <span class="k">if</span> <span class="n">qos</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]:</span>
<a name="MQTTBrokers.py-pyg.html-90"></a>      <span class="n">pub</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">msgid</span>
<a name="MQTTBrokers.py-pyg.html-91"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;client id: </span><span class="si">%d</span><span class="s"> msgid: </span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">msgid</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-92"></a>      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">msgid</span> <span class="o">==</span> <span class="mi">65535</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-93"></a>        <span class="bp">self</span><span class="o">.</span><span class="n">msgid</span> <span class="o">=</span> <span class="mi">1</span>
<a name="MQTTBrokers.py-pyg.html-94"></a>      <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-95"></a>        <span class="bp">self</span><span class="o">.</span><span class="n">msgid</span> <span class="o">+=</span> <span class="mi">1</span>
<a name="MQTTBrokers.py-pyg.html-96"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">outbound</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pub</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-97"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span><span class="p">[</span><span class="n">pub</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">]</span> <span class="o">=</span> <span class="n">pub</span>
<a name="MQTTBrokers.py-pyg.html-98"></a>    <span class="k">if</span> <span class="n">qos</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">dropQoS0</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-99"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">outbound</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pub</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-100"></a>    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">connected</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-101"></a>      <span class="n">respond</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">socket</span><span class="p">,</span> <span class="n">pub</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-102"></a>    <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-103"></a>      <span class="k">if</span> <span class="n">qos</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]:</span>
<a name="MQTTBrokers.py-pyg.html-104"></a>        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.2-5] storing of QoS 1 and 2 messagesfor disconnected client </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-105"></a>
<a name="MQTTBrokers.py-pyg.html-106"></a>  <span class="k">def</span> <span class="nf">puback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msgid</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-107"></a>    <span class="k">if</span> <span class="n">msgid</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<a name="MQTTBrokers.py-pyg.html-108"></a>      <span class="n">pub</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span><span class="p">[</span><span class="n">msgid</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-109"></a>      <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-110"></a>        <span class="bp">self</span><span class="o">.</span><span class="n">outbound</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">pub</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-111"></a>        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span><span class="p">[</span><span class="n">msgid</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-112"></a>      <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-113"></a>        <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: Puback received for msgid </span><span class="si">%d</span><span class="s">, but QoS is </span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">msgid</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-114"></a>    <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-115"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: Puback received for msgid </span><span class="si">%d</span><span class="s">, but no message found&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">msgid</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-116"></a>
<a name="MQTTBrokers.py-pyg.html-117"></a>  <span class="k">def</span> <span class="nf">pubrec</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msgid</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-118"></a>    <span class="n">rc</span> <span class="o">=</span> <span class="bp">False</span>
<a name="MQTTBrokers.py-pyg.html-119"></a>    <span class="k">if</span> <span class="n">msgid</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<a name="MQTTBrokers.py-pyg.html-120"></a>      <span class="n">pub</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span><span class="p">[</span><span class="n">msgid</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-121"></a>      <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-122"></a>        <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">qos2state</span> <span class="o">==</span> <span class="s">&quot;PUBREC&quot;</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-123"></a>          <span class="n">pub</span><span class="o">.</span><span class="n">qos2state</span> <span class="o">=</span> <span class="s">&quot;PUBCOMP&quot;</span>
<a name="MQTTBrokers.py-pyg.html-124"></a>          <span class="n">rc</span> <span class="o">=</span> <span class="bp">True</span>
<a name="MQTTBrokers.py-pyg.html-125"></a>        <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-126"></a>          <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: Pubrec received for msgid </span><span class="si">%d</span><span class="s">, but message in wrong state&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">msgid</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-127"></a>      <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-128"></a>        <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: Pubrec received for msgid </span><span class="si">%d</span><span class="s">, but QoS is </span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">msgid</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-129"></a>    <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-130"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: Pubrec received for msgid </span><span class="si">%d</span><span class="s">, but no message found&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">msgid</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-131"></a>    <span class="k">return</span> <span class="n">rc</span>
<a name="MQTTBrokers.py-pyg.html-132"></a>
<a name="MQTTBrokers.py-pyg.html-133"></a>  <span class="k">def</span> <span class="nf">pubcomp</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msgid</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-134"></a>    <span class="k">if</span> <span class="n">msgid</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<a name="MQTTBrokers.py-pyg.html-135"></a>      <span class="n">pub</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span><span class="p">[</span><span class="n">msgid</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-136"></a>      <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-137"></a>        <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">qos2state</span> <span class="o">==</span> <span class="s">&quot;PUBCOMP&quot;</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-138"></a>          <span class="bp">self</span><span class="o">.</span><span class="n">outbound</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">pub</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-139"></a>          <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">outmsgs</span><span class="p">[</span><span class="n">msgid</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-140"></a>        <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-141"></a>          <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Pubcomp received for msgid </span><span class="si">%d</span><span class="s">, but message in wrong state&quot;</span><span class="p">,</span> <span class="n">msgid</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-142"></a>      <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-143"></a>        <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Pubcomp received for msgid </span><span class="si">%d</span><span class="s">, but QoS is </span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">msgid</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-144"></a>    <span class="k">else</span><span class="p">:</span>  
<a name="MQTTBrokers.py-pyg.html-145"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Pubcomp received for msgid </span><span class="si">%d</span><span class="s">, but no message found&quot;</span><span class="p">,</span> <span class="n">msgid</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-146"></a>
<a name="MQTTBrokers.py-pyg.html-147"></a>  <span class="k">def</span> <span class="nf">pubrel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msgid</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-148"></a>    <span class="n">rc</span> <span class="o">=</span> <span class="bp">None</span>
<a name="MQTTBrokers.py-pyg.html-149"></a>    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">publish_on_pubrel</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-150"></a>        <span class="n">pub</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inbound</span><span class="p">[</span><span class="n">msgid</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-151"></a>        <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-152"></a>          <span class="n">rc</span> <span class="o">=</span> <span class="n">pub</span>
<a name="MQTTBrokers.py-pyg.html-153"></a>        <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-154"></a>          <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Pubrec received for msgid </span><span class="si">%d</span><span class="s">, but QoS is </span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">msgid</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-155"></a>    <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-156"></a>      <span class="n">rc</span> <span class="o">=</span> <span class="n">msgid</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">inbound</span>
<a name="MQTTBrokers.py-pyg.html-157"></a>    <span class="k">if</span> <span class="ow">not</span> <span class="n">rc</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-158"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Pubrec received for msgid </span><span class="si">%d</span><span class="s">, but no message found&quot;</span><span class="p">,</span> <span class="n">msgid</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-159"></a>    <span class="k">return</span> <span class="n">rc</span> 
<a name="MQTTBrokers.py-pyg.html-160"></a>  
<a name="MQTTBrokers.py-pyg.html-161"></a>
<a name="MQTTBrokers.py-pyg.html-162"></a><span class="k">class</span> <span class="nc">MQTTBrokers</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-163"></a>
<a name="MQTTBrokers.py-pyg.html-164"></a>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">publish_on_pubrel</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">overlapping_single</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">dropQoS0</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-165"></a>
<a name="MQTTBrokers.py-pyg.html-166"></a>    <span class="c"># optional behaviour</span>
<a name="MQTTBrokers.py-pyg.html-167"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">publish_on_pubrel</span> <span class="o">=</span> <span class="n">publish_on_pubrel</span>
<a name="MQTTBrokers.py-pyg.html-168"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">dropQoS0</span> <span class="o">=</span> <span class="n">dropQoS0</span>                    <span class="c"># don&#39;t queue QoS 0 messages for disconnected clients</span>
<a name="MQTTBrokers.py-pyg.html-169"></a>
<a name="MQTTBrokers.py-pyg.html-170"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">broker</span> <span class="o">=</span> <span class="n">Brokers</span><span class="p">(</span><span class="n">overlapping_single</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-171"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">clients</span> <span class="o">=</span> <span class="p">{}</span>   <span class="c"># socket -&gt; clients</span>
<a name="MQTTBrokers.py-pyg.html-172"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">RLock</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-173"></a>
<a name="MQTTBrokers.py-pyg.html-174"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;MQTT 3.1.1 Paho Test Broker&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-175"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Optional behaviour, publish on pubrel: </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">publish_on_pubrel</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-176"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Optional behaviour, single publish on overlapping topics: </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">overlapping_single</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-177"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Optional behaviour, drop QoS 0 publications to disconnected clients: </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dropQoS0</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-178"></a>
<a name="MQTTBrokers.py-pyg.html-179"></a>
<a name="MQTTBrokers.py-pyg.html-180"></a>  <span class="k">def</span> <span class="nf">reinitialize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-181"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Reinitializing broker&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-182"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">clients</span> <span class="o">=</span> <span class="p">{}</span>   
<a name="MQTTBrokers.py-pyg.html-183"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">reinitialize</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-184"></a>
<a name="MQTTBrokers.py-pyg.html-185"></a>  <span class="k">def</span> <span class="nf">handleRequest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-186"></a>    <span class="s">&quot;this is going to be called from multiple threads, so synchronize&quot;</span>
<a name="MQTTBrokers.py-pyg.html-187"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">lock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-188"></a>    <span class="n">terminate</span> <span class="o">=</span> <span class="bp">False</span>
<a name="MQTTBrokers.py-pyg.html-189"></a>    <span class="k">try</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-190"></a>      <span class="n">raw_packet</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">getPacket</span><span class="p">(</span><span class="n">sock</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-191"></a>      <span class="k">if</span> <span class="n">raw_packet</span> <span class="o">==</span> <span class="bp">None</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-192"></a>        <span class="c"># will message</span>
<a name="MQTTBrokers.py-pyg.html-193"></a>        <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="n">terminate</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-194"></a>        <span class="n">terminate</span> <span class="o">=</span> <span class="bp">True</span>
<a name="MQTTBrokers.py-pyg.html-195"></a>      <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-196"></a>        <span class="n">packet</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">unpackPacket</span><span class="p">(</span><span class="n">raw_packet</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-197"></a>        <span class="k">if</span> <span class="n">packet</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-198"></a>          <span class="n">terminate</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">handlePacket</span><span class="p">(</span><span class="n">packet</span><span class="p">,</span> <span class="n">sock</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-199"></a>        <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-200"></a>          <span class="k">raise</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">MQTTException</span><span class="p">(</span><span class="s">&quot;[MQTT-2.0.0-1] handleRequest: badly formed MQTT packet&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-201"></a>    <span class="k">finally</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-202"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">lock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-203"></a>    <span class="k">return</span> <span class="n">terminate</span>
<a name="MQTTBrokers.py-pyg.html-204"></a>
<a name="MQTTBrokers.py-pyg.html-205"></a>  <span class="k">def</span> <span class="nf">handlePacket</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">packet</span><span class="p">,</span> <span class="n">sock</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-206"></a>    <span class="n">terminate</span> <span class="o">=</span> <span class="bp">False</span>
<a name="MQTTBrokers.py-pyg.html-207"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;in: &quot;</span><span class="o">+</span><span class="nb">repr</span><span class="p">(</span><span class="n">packet</span><span class="p">))</span>
<a name="MQTTBrokers.py-pyg.html-208"></a>    <span class="k">if</span> <span class="n">sock</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span> <span class="ow">and</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">MessageType</span> <span class="o">!=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">CONNECT</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-209"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-210"></a>      <span class="k">raise</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">MQTTException</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.0-1] Connect was not first packet on socket&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-211"></a>    <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-212"></a>      <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">packetNames</span><span class="p">[</span><span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">MessageType</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">())(</span><span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-213"></a>      <span class="k">if</span> <span class="n">sock</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<a name="MQTTBrokers.py-pyg.html-214"></a>        <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span><span class="o">.</span><span class="n">lastPacket</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-215"></a>    <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">MessageType</span> <span class="o">==</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">DISCONNECT</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-216"></a>      <span class="n">terminate</span> <span class="o">=</span> <span class="bp">True</span>
<a name="MQTTBrokers.py-pyg.html-217"></a>    <span class="k">return</span> <span class="n">terminate</span>
<a name="MQTTBrokers.py-pyg.html-218"></a>
<a name="MQTTBrokers.py-pyg.html-219"></a>  <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-220"></a>    <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">ProtocolName</span> <span class="o">!=</span> <span class="s">&quot;MQTT&quot;</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-221"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-222"></a>      <span class="k">raise</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">MQTTException</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.2-1] Wrong protocol name </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">packet</span><span class="o">.</span><span class="n">ProtocolName</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-223"></a>    <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">ProtocolVersion</span> <span class="o">!=</span> <span class="mi">4</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-224"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.2-2] Wrong protocol version </span><span class="si">%d</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">ProtocolVersion</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-225"></a>      <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Connacks</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-226"></a>      <span class="n">resp</span><span class="o">.</span><span class="n">returnCode</span> <span class="o">=</span> <span class="mi">1</span>
<a name="MQTTBrokers.py-pyg.html-227"></a>      <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-228"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-229"></a>      <span class="k">return</span>
<a name="MQTTBrokers.py-pyg.html-230"></a>    <span class="k">if</span> <span class="n">sock</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>    <span class="c"># is socket is already connected?</span>
<a name="MQTTBrokers.py-pyg.html-231"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-232"></a>      <span class="k">raise</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">MQTTException</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.0-2] Second connect packet&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-233"></a>    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">ClientIdentifier</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">packet</span><span class="o">.</span><span class="n">Cleansession</span> <span class="o">==</span> <span class="bp">False</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-234"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.3-8] Reject 0-length clientid with cleansession false&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-235"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.3-9] if clientid is rejected, must send connack 2 and close connection&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-236"></a>      <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Connacks</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-237"></a>      <span class="n">resp</span><span class="o">.</span><span class="n">returnCode</span> <span class="o">=</span> <span class="mi">2</span>
<a name="MQTTBrokers.py-pyg.html-238"></a>      <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-239"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-240"></a>      <span class="k">return</span>
<a name="MQTTBrokers.py-pyg.html-241"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.3-5] Clientids of 1 to 23 chars and ascii alphanumeric must be allowed&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-242"></a>    <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">ClientIdentifier</span> <span class="ow">in</span> <span class="p">[</span><span class="n">client</span><span class="o">.</span><span class="n">id</span> <span class="k">for</span> <span class="n">client</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="o">.</span><span class="n">values</span><span class="p">()]:</span> <span class="c"># is this client already connected on a different socket?</span>
<a name="MQTTBrokers.py-pyg.html-243"></a>      <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<a name="MQTTBrokers.py-pyg.html-244"></a>        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">s</span><span class="p">]</span> <span class="o">==</span> <span class="n">packet</span><span class="o">.</span><span class="n">ClientIdentifier</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-245"></a>          <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.4-2] Disconnecting old client </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">ClientIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-246"></a>          <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-247"></a>          <span class="k">break</span>
<a name="MQTTBrokers.py-pyg.html-248"></a>    <span class="n">me</span> <span class="o">=</span> <span class="bp">None</span>
<a name="MQTTBrokers.py-pyg.html-249"></a>    <span class="k">if</span> <span class="ow">not</span> <span class="n">packet</span><span class="o">.</span><span class="n">CleanSession</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-250"></a>      <span class="n">me</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">getClient</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">ClientIdentifier</span><span class="p">)</span> <span class="c"># find existing state, if there is any</span>
<a name="MQTTBrokers.py-pyg.html-251"></a>      <span class="k">if</span> <span class="n">me</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-252"></a>        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.3-2] clientid used to retrieve client state&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-253"></a>    <span class="k">if</span> <span class="n">me</span> <span class="o">==</span> <span class="bp">None</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-254"></a>      <span class="n">me</span> <span class="o">=</span> <span class="n">MQTTClients</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">ClientIdentifier</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">CleanSession</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">KeepAliveTimer</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-255"></a>    <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-256"></a>      <span class="n">me</span><span class="o">.</span><span class="n">socket</span> <span class="o">=</span> <span class="n">sock</span> <span class="c"># set existing client state to new socket</span>
<a name="MQTTBrokers.py-pyg.html-257"></a>      <span class="n">me</span><span class="o">.</span><span class="n">cleansession</span> <span class="o">=</span> <span class="n">packet</span><span class="o">.</span><span class="n">CleanSession</span>
<a name="MQTTBrokers.py-pyg.html-258"></a>      <span class="n">me</span><span class="o">.</span><span class="n">keepalive</span> <span class="o">=</span> <span class="n">packet</span><span class="o">.</span><span class="n">KeepAliveTimer</span>
<a name="MQTTBrokers.py-pyg.html-259"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span> <span class="o">=</span> <span class="n">me</span>
<a name="MQTTBrokers.py-pyg.html-260"></a>    <span class="n">me</span><span class="o">.</span><span class="n">will</span> <span class="o">=</span> <span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">WillTopic</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">WillQoS</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">WillMessage</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">WillRETAIN</span><span class="p">)</span> <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">WillFlag</span> <span class="k">else</span> <span class="bp">None</span>
<a name="MQTTBrokers.py-pyg.html-261"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">me</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-262"></a>    <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Connacks</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-263"></a>    <span class="n">resp</span><span class="o">.</span><span class="n">returnCode</span> <span class="o">=</span> <span class="mi">0</span>
<a name="MQTTBrokers.py-pyg.html-264"></a>    <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-265"></a>    <span class="n">me</span><span class="o">.</span><span class="n">resend</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-266"></a>
<a name="MQTTBrokers.py-pyg.html-267"></a>  <span class="k">def</span> <span class="nf">disconnect</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">,</span> <span class="n">terminate</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-268"></a>    <span class="k">if</span> <span class="n">sock</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<a name="MQTTBrokers.py-pyg.html-269"></a>      <span class="k">if</span> <span class="n">terminate</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-270"></a>        <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">terminate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-271"></a>      <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-272"></a>        <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-273"></a>      <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-274"></a>    <span class="k">try</span><span class="p">:</span>   
<a name="MQTTBrokers.py-pyg.html-275"></a>      <span class="n">sock</span><span class="o">.</span><span class="n">shutdown</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">SHUT_RDWR</span><span class="p">)</span> <span class="c"># must call shutdown to close socket immediately</span>
<a name="MQTTBrokers.py-pyg.html-276"></a>    <span class="k">except</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-277"></a>      <span class="k">pass</span> <span class="c"># doesn&#39;t matter if the socket has been closed at the other end already</span>
<a name="MQTTBrokers.py-pyg.html-278"></a>    <span class="k">try</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-279"></a>      <span class="n">sock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-280"></a>    <span class="k">except</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-281"></a>      <span class="k">pass</span> <span class="c"># doesn&#39;t matter if the socket has been closed at the other end already</span>
<a name="MQTTBrokers.py-pyg.html-282"></a>
<a name="MQTTBrokers.py-pyg.html-283"></a>  <span class="k">def</span> <span class="nf">disconnectAll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-284"></a>    <span class="k">for</span> <span class="n">sock</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<a name="MQTTBrokers.py-pyg.html-285"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-286"></a>
<a name="MQTTBrokers.py-pyg.html-287"></a>  <span class="k">def</span> <span class="nf">subscribe</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-288"></a>    <span class="n">topics</span> <span class="o">=</span> <span class="p">[]</span>
<a name="MQTTBrokers.py-pyg.html-289"></a>    <span class="n">qoss</span> <span class="o">=</span> <span class="p">[]</span>
<a name="MQTTBrokers.py-pyg.html-290"></a>    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">packet</span><span class="o">.</span><span class="n">data</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-291"></a>      <span class="n">topics</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<a name="MQTTBrokers.py-pyg.html-292"></a>      <span class="n">qoss</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<a name="MQTTBrokers.py-pyg.html-293"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">subscribe</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">topics</span><span class="p">,</span> <span class="n">qoss</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-294"></a>    <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Subacks</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-295"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.3.1-7][MQTT-3.8.4-2] Suback has same message id as subscribe&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-296"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.8.4-1] Must respond with suback&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-297"></a>    <span class="n">resp</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="o">=</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span>
<a name="MQTTBrokers.py-pyg.html-298"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.8.4-5] return code must be returned for each topic in subscribe&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-299"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.9.3-1] the order of return codes must match order of topics in subscribe&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-300"></a>    <span class="n">resp</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">qoss</span>
<a name="MQTTBrokers.py-pyg.html-301"></a>    <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-302"></a>
<a name="MQTTBrokers.py-pyg.html-303"></a>  <span class="k">def</span> <span class="nf">unsubscribe</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-304"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">unsubscribe</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-305"></a>    <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Unsubacks</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-306"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.3.1-7] Unsuback has same message id as unsubscribe&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-307"></a>    <span class="n">resp</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="o">=</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span>
<a name="MQTTBrokers.py-pyg.html-308"></a>    <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-309"></a>
<a name="MQTTBrokers.py-pyg.html-310"></a>  <span class="k">def</span> <span class="nf">publish</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-311"></a>    <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">topicName</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&quot;+&quot;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="ow">or</span> <span class="n">packet</span><span class="o">.</span><span class="n">topicName</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&quot;#&quot;</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-312"></a>      <span class="k">raise</span> <span class="n">MqttException</span><span class="p">(</span><span class="s">&quot;[MQTT-3.3.2-2][MQTT-4.7.1-1] wildcards not allowed in topic name&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-313"></a>    <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-314"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span><span class="o">.</span><span class="n">id</span><span class="p">,</span>
<a name="MQTTBrokers.py-pyg.html-315"></a>             <span class="n">packet</span><span class="o">.</span><span class="n">topicName</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">RETAIN</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-316"></a>    <span class="k">elif</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-317"></a>      <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">DUP</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-318"></a>        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.1.2-5] Incoming publish DUP 1 ==&gt; outgoing publish with DUP 0&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-319"></a>      <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span><span class="o">.</span><span class="n">id</span><span class="p">,</span>
<a name="MQTTBrokers.py-pyg.html-320"></a>             <span class="n">packet</span><span class="o">.</span><span class="n">topicName</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">RETAIN</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-321"></a>      <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Pubacks</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-322"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.3.1-6] puback messge id same as publish&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-323"></a>      <span class="n">resp</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="o">=</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span>
<a name="MQTTBrokers.py-pyg.html-324"></a>      <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-325"></a>    <span class="k">elif</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-326"></a>      <span class="n">myclient</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-327"></a>      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">publish_on_pubrel</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-328"></a>        <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="ow">in</span> <span class="n">myclient</span><span class="o">.</span><span class="n">inbound</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<a name="MQTTBrokers.py-pyg.html-329"></a>          <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">DUP</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-330"></a>            <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;[MQTT-2.1.2-2] duplicate QoS 2 message id </span><span class="si">%d</span><span class="s"> found with DUP 0&quot;</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-331"></a>          <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-332"></a>            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.1.2-2] DUP flag is 1 on redelivery&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-333"></a>        <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-334"></a>          <span class="n">myclient</span><span class="o">.</span><span class="n">inbound</span><span class="p">[</span><span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">]</span> <span class="o">=</span> <span class="n">packet</span>
<a name="MQTTBrokers.py-pyg.html-335"></a>      <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-336"></a>        <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="ow">in</span> <span class="n">myclient</span><span class="o">.</span><span class="n">inbound</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-337"></a>          <span class="k">if</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">DUP</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-338"></a>            <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;[MQTT-2.1.2-2] duplicate QoS 2 message id </span><span class="si">%d</span><span class="s"> found with DUP 0&quot;</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-339"></a>          <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-340"></a>            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.1.2-2] DUP flag is 1 on redelivery&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-341"></a>        <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-342"></a>          <span class="n">myclient</span><span class="o">.</span><span class="n">inbound</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-343"></a>          <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="n">myclient</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">topicName</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span><span class="p">,</span> <span class="n">packet</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">RETAIN</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-344"></a>      <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Pubrecs</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-345"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.3.1-6] pubrec messge id same as publish&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-346"></a>      <span class="n">resp</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="o">=</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span>
<a name="MQTTBrokers.py-pyg.html-347"></a>      <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-348"></a>
<a name="MQTTBrokers.py-pyg.html-349"></a>  <span class="k">def</span> <span class="nf">pubrel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-350"></a>    <span class="n">myclient</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-351"></a>    <span class="n">pub</span> <span class="o">=</span> <span class="n">myclient</span><span class="o">.</span><span class="n">pubrel</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-352"></a>    <span class="k">if</span> <span class="n">pub</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-353"></a>      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">publish_on_pubrel</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-354"></a>        <span class="bp">self</span><span class="o">.</span><span class="n">broker</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="n">myclient</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">topicName</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">QoS</span><span class="p">,</span> <span class="n">pub</span><span class="o">.</span><span class="n">fh</span><span class="o">.</span><span class="n">RETAIN</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-355"></a>        <span class="k">del</span> <span class="n">myclient</span><span class="o">.</span><span class="n">inbound</span><span class="p">[</span><span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-356"></a>      <span class="k">else</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-357"></a>        <span class="n">myclient</span><span class="o">.</span><span class="n">inbound</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-358"></a>    <span class="c"># must respond with pubcomp regardless of whether a message was found</span>
<a name="MQTTBrokers.py-pyg.html-359"></a>    <span class="k">if</span> <span class="ow">not</span> <span class="n">pub</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-360"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.6.4-1] must respond with a pubcomp packet&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-361"></a>    <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Pubcomps</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-362"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-2.3.1-6] pubcomp messge id same as publish&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-363"></a>    <span class="n">resp</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="o">=</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span>
<a name="MQTTBrokers.py-pyg.html-364"></a>    <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-365"></a>
<a name="MQTTBrokers.py-pyg.html-366"></a>  <span class="k">def</span> <span class="nf">pingreq</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-367"></a>    <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Pingresps</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-368"></a>    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.12.4-1] sending pingresp in response to pingreq&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-369"></a>    <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-370"></a>
<a name="MQTTBrokers.py-pyg.html-371"></a>  <span class="k">def</span> <span class="nf">puback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-372"></a>    <span class="s">&quot;confirmed reception of qos 1&quot;</span>
<a name="MQTTBrokers.py-pyg.html-373"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span><span class="o">.</span><span class="n">puback</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-374"></a>
<a name="MQTTBrokers.py-pyg.html-375"></a>  <span class="k">def</span> <span class="nf">pubrec</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-376"></a>    <span class="s">&quot;confirmed reception of qos 2&quot;</span>
<a name="MQTTBrokers.py-pyg.html-377"></a>    <span class="n">myclient</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-378"></a>    <span class="k">if</span> <span class="n">myclient</span><span class="o">.</span><span class="n">pubrec</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-379"></a>      <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.5.4-1] must reply with pubrel in response to pubrec&quot;</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-380"></a>      <span class="n">resp</span> <span class="o">=</span> <span class="n">MQTTV3</span><span class="o">.</span><span class="n">Pubrels</span><span class="p">()</span>
<a name="MQTTBrokers.py-pyg.html-381"></a>      <span class="n">resp</span><span class="o">.</span><span class="n">messageIdentifier</span> <span class="o">=</span> <span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span>
<a name="MQTTBrokers.py-pyg.html-382"></a>      <span class="n">respond</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">resp</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-383"></a>
<a name="MQTTBrokers.py-pyg.html-384"></a>  <span class="k">def</span> <span class="nf">pubcomp</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-385"></a>    <span class="s">&quot;confirmed reception of qos 2&quot;</span>
<a name="MQTTBrokers.py-pyg.html-386"></a>    <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span><span class="o">.</span><span class="n">pubcomp</span><span class="p">(</span><span class="n">packet</span><span class="o">.</span><span class="n">messageIdentifier</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-387"></a>
<a name="MQTTBrokers.py-pyg.html-388"></a>  <span class="k">def</span> <span class="nf">keepalive</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sock</span><span class="p">):</span>
<a name="MQTTBrokers.py-pyg.html-389"></a>    <span class="k">if</span> <span class="n">sock</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<a name="MQTTBrokers.py-pyg.html-390"></a>      <span class="n">client</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clients</span><span class="p">[</span><span class="n">sock</span><span class="p">]</span>
<a name="MQTTBrokers.py-pyg.html-391"></a>      <span class="k">if</span> <span class="n">client</span><span class="o">.</span><span class="n">keepalive</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">client</span><span class="o">.</span><span class="n">lastPacket</span> <span class="o">&gt;</span> <span class="n">client</span><span class="o">.</span><span class="n">keepalive</span> <span class="o">*</span> <span class="mf">1.5</span><span class="p">:</span>
<a name="MQTTBrokers.py-pyg.html-392"></a>        <span class="c"># keep alive timeout</span>
<a name="MQTTBrokers.py-pyg.html-393"></a>        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;[MQTT-3.1.2-22] keepalive timeout for client </span><span class="si">%s</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">client</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<a name="MQTTBrokers.py-pyg.html-394"></a>        <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="n">terminate</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>

</pre>

    </body>
</html>